import {_decorator, Component} from 'cc';
import FSMManager from "db://assets/scripts/state/FSMManager";
import {BirdState} from "db://assets/scripts/state/BirdState";
import FlyState from "db://assets/scripts/state/FlyState";
import DieState from "db://assets/scripts/state/DieState";

const {ccclass, property} = _decorator;

@ccclass('BirdController')
export class BirdController extends Component {
    /**
     * 状态机
     */
    fsmManager: FSMManager;

    start() {
        // 初始化状态机
        this.fsmManager = new FSMManager();
        // 添加状态
        let flyState = new FlyState(BirdState.FLY, this, this.fsmManager);
        let dieState = new DieState(BirdState.DIE, this, this.fsmManager);
        this.fsmManager.states.push(flyState, dieState);
        // 切换状态
        this.fsmManager.changeState(BirdState.FLY);
    }

    update(deltaTime: number) {
        if (this.fsmManager.currentStateIndex !== -1) {
            this.fsmManager.states[this.fsmManager.currentStateIndex].onUpdate();
        }
    }

    fly() {
        this.fsmManager.changeState(BirdState.FLY);
        // this.node.getComponent(Animation).play("bird_fly");
    }

    die() {
        this.fsmManager.changeState(BirdState.DIE);
        // this.node.getComponent(Animation).play("bird_die");
    }
}

